package com.chen.nongansampling.presenter;

import android.content.Context;
import android.util.Log;

import com.chen.nongansampling.bean.Callback;
import com.chen.nongansampling.bean.SimpleRequest;
import com.chen.nongansampling.bean.getOperatingType;
import com.chen.nongansampling.bean.request.InsertOperatingRequest;
import com.chen.nongansampling.bean.sample.TypeList;
import com.chen.nongansampling.manager.DataManager;
import com.chen.nongansampling.model.CallbackData;
import com.chen.nongansampling.model.Market;
import com.chen.nongansampling.presenterView.AddOperatorView;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AddOperatorPresenter extends BasePresenter {
    private Context mContext;
    private AddOperatorView baseView;
    private CallbackData<List<Market>> callbackData;
    private Callback callback;
    private CallbackData<List<TypeList>> typelist;
    private CallbackData<List<getOperatingType>> callbackDatatype;
    public AddOperatorPresenter(Context context)
    {
        this.mContext = context;
    }


    @Override
    public void BindPresentView(Object presentView) {
        baseView = (AddOperatorView) presentView;
    }

    public void getAddOperatorPresenterResponseInfo(SimpleRequest simpleRequest) {
        DataManager.getInstance(mContext).getMarketResponseInfo(simpleRequest)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<CallbackData<List<Market>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CallbackData<List<Market>> accountCallbackData) {

                        callbackData = accountCallbackData;
                    }



                    @Override
                    public void onError(Throwable e) {
                        baseView.onError(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        if (callbackData != null) {
                            baseView.getOperatorLists(callbackData);

                        }
                        //SamplePresenter.super.compositeDisposable.dispose();
                    }
                });
    }

    public void getAddOperatortype(SimpleRequest simpleRequest) {
        DataManager.getInstance(mContext).getAddOperatortype(simpleRequest)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<CallbackData<List<getOperatingType>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CallbackData<List<getOperatingType>> accountCallbackData) {

                        callbackDatatype = accountCallbackData;
                    }



                    @Override
                    public void onError(Throwable e) {
                        baseView.onError(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        if (callbackDatatype != null) {
                            baseView.getOperatorListtype(callbackDatatype);

                        }
                        //SamplePresenter.super.compositeDisposable.dispose();
                    }
                });
    }

    public void getAddOperatorPresenterResponseInfo(InsertOperatingRequest insertOperatingRequest) {
        DataManager.getInstance(mContext).insertOperatingResponseInfo(insertOperatingRequest)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Callback>(){
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Callback listCallbackData) {
                        callback = listCallbackData;

                    }


                    @Override
                    public void onError(Throwable e) {
                        baseView.onError(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        if (callback != null) {
                            baseView.insertOperator(callback);

                        }
                        //                      AddOperatorPresenter.super.compositeDisposable.dispose();
                    }
                });

    }


    public void getSamplingNameResponseInfo(SimpleRequest simpleRequest) {    //获取人员名称
        DataManager.getInstance(mContext).getSamplingNameResponseInfo(simpleRequest)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<CallbackData<List<TypeList>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CallbackData<List<TypeList>> listCallbackData) {
                        typelist = listCallbackData;

                    }

//                    @Override
//                    public void onNext(CallbackData<List<OperatingBean>> accountCallbackData) {
//
//                        operatorBeanList = accountCallbackData;
//                    }



                    @Override
                    public void onError(Throwable e) {
                        baseView.onError(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        if (typelist != null) {
                            baseView.getSamplingName(typelist);

                            Log.d("typelist",typelist.getData().size()+"");


                            //              Log.d("typelist",typelist.getData().get(0).getList().get(0).getName()+"");



                            Log.d("typelist",typelist.toString());
                        }

                    }
                });
    }
}
